package com.gz.logistics.app;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.gz.logistics.R;
import com.gz.logistics.library.IAuthor;
import com.gz.logistics.library.listener.LoginListener;
import com.gz.logistics.library.pojo.UserInfo;
import com.gz.logistics.library.util.Tools;
import com.gz.logistics.util.ShareUtil;
import com.gz.logistics.util.ToastUtils;

public class LoginActivity extends Activity implements View.OnClickListener {
    private static final  String TAG = LoginActivity.class.getSimpleName();
    private EditText et_login_name;
    private EditText et_login_pwd;
    private Button bt_login;
    private LoginListener loginListener;
    private Dialog mLoadingData;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
        initListener();
    }



    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_login_delivery:
                login();
                break;
            default:
                break;
        }
    }

    public void initView(){
        et_login_name = (EditText) findViewById(R.id.et_login_name);
        et_login_pwd = (EditText) findViewById(R.id.et_login_password);
        bt_login = (Button) findViewById(R.id.btn_login_delivery);
        mLoadingData = new Dialog(this,R.style.DialogTheme);
        mLoadingData.setContentView(R.layout.loading_data_dialog2);
        mLoadingData.setCanceledOnTouchOutside(false);
        mLoadingData.setCancelable(false);
        bt_login.setOnClickListener(this);
    }

    private void initListener() {
        loginListener = new LoginListener() {
            @Override
            public void onResponseSuccess(UserInfo userInfo) {

                mLoadingData.dismiss();
                if (userInfo.getUser_type() != Define.IDENTIFY_DELIVERYER){
                    ToastUtils.showToast(getApplicationContext(),"用户身份不正确");
                    return;
                }else{
                    IAuthor.getInstance().saveToDB(userInfo);
                    ShareUtil.setstringvalue(LoginActivity.this,"user_number",et_login_name.getText().toString().trim());
                    ShareUtil.setstringvalue(LoginActivity.this,"uid",userInfo.getUser_id()+"");
                    Log.i("duzhijuan",ShareUtil.getstringvalue(LoginActivity.this,"user_number"));
                    Intent exit = new Intent(Define.ACTION_EXIT_WHOLE_APP);
                    sendBroadcast(exit);
                    startActivity(new Intent().setClass(getApplicationContext(), MainActivity.class));
                    finish();
                }

            }

            @Override
            public void userNameOrPwdError() {
                mLoadingData.dismiss();
                ToastUtils.showToast(getApplicationContext(),Define.MSG_ERROR_USERNAME_OR_PWD);
            }
            @Override
            public void userForbidden() {
                mLoadingData.dismiss();
                ToastUtils.showToast(getApplicationContext(),Define.MSG_USER_FORBIDDEN);
            }

            @Override
            public void onNoNetWork() {
                mLoadingData.dismiss();
                ToastUtils.showToast(getApplicationContext(),Define.MSG_NO_NETWORK);
            }

            @Override
            public void onResponseFailed() {
                mLoadingData.dismiss();
                ToastUtils.showToast(getApplicationContext(),Define.MSG_LOGIN_FAILED);
            }
        };
    }

    public void login(){
        String userName = et_login_name.getText().toString().trim();
        String pwd = et_login_pwd.getText().toString();
        if (TextUtils.isEmpty(userName)){
            ToastUtils.showToast(this,"请输入用户名");
            return;
        }
        if (TextUtils.isEmpty(pwd)){
            ToastUtils.showToast(this,"密码不能为空");
            return;
        }
        mLoadingData.show();
        if (!Tools.hasInternetConnection(getApplicationContext())){
            mLoadingData.dismiss();
            ToastUtils.showToast(getApplicationContext(),Define.MSG_NO_NETWORK);
            return ;
        }

        IAuthor.getInstance().login(userName, pwd, loginListener);

    }


}
